package edu.gxnu.cetcdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.gxnu.cetcdemo.mapper.SuAdminMapper;
import edu.gxnu.cetcdemo.pojo.SuAdmin;
import edu.gxnu.cetcdemo.service.SuAdminService;
import edu.gxnu.cetcdemo.utils.JwtHelper;
import edu.gxnu.cetcdemo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
* @author Uplifted
* @description 针对表【su_admin】的数据库操作Service实现
* @createDate 2024-04-02 15:09:11
*/
@Service
public class SuAdminServiceImpl extends ServiceImpl<SuAdminMapper, SuAdmin>
    implements SuAdminService{

    @Autowired
    private SuAdminMapper suAdminMapper;

    @Autowired
    private JwtHelper jwtHelper;

    @Override
    public Result login(SuAdmin suAdmin) {
        // 根据用户名查询数据库
        LambdaQueryWrapper<SuAdmin> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        SuAdmin loginSuAdmin = suAdminMapper.selectOne(lambdaQueryWrapper.eq(SuAdmin::getSuAdminAccount, suAdmin.getSuAdminAccount()));

        // 若用户名不存在
        if (loginSuAdmin == null) {
            return Result.build(501, "超管账号不存在");
        }else{
            // 比较密码
            if (suAdmin.getSuAdminPassword().equals(loginSuAdmin.getSuAdminPassword())) {
                // 登录成功
                // 生成token
                String token = JwtHelper.getToken(loginSuAdmin.getSuAdminId().toString());
                // 返回用户数据和token
                Map data = new HashMap();
                data.put("token", token);
                data.put("suadmin", loginSuAdmin);

                return Result.build(200, "登录成功", data);
            }else{
                // 登录失败
                return Result.build(502, "密码错误");
            }
        }
    }
}




